<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Category extends Controller_Admin_Based
{

    private $id_parent;
    private $id_top;
    private $categories;
    private $categories_norec;

    public function before()
    {
        parent::before();
        $id_parent = $this->request->param('id');
        $categories = Category::instances()->get_categories();

        if ($id_parent > 0)
        {
            $category_norec = Category::instances()->get_categories_norec($id_parent);
            $arr_category_parent = Category::instances()->get_a_category($id_parent);
            $id_top = intval($arr_category_parent['id_parent']);
        }
        else
        {
            $category_norec = Category::instances()->get_categories_norec(0);
            $id_top = null;
        }

        $this->id_parent = $id_parent;
        $this->categories = $categories;
        $this->categories_norec = $category_norec;
        $this->id_top = $id_top;
    }

    public function action_list()
    {

        $arr_category = $this->categories_norec;
        $data = array(
            'result' => $arr_category,
            'id_top' => $this->id_top,
            'id' => $this->id_parent,
        );
        $view = Viewer::display('admin/category_list', $data);
        $this->response->body($view);
    }

    public function action_add()
    {
        
        $id = $this->request->param('id');
        
        if($id == '') {
            $id = 0;
        }
        
        $data = array(
            'categories_parent' => $this->categories,
            'id'=>$id,
        );
        $view = Viewer::display('admin/category_add', $data);
        $this->response->body($view);
    }

    public function action_do_add()
    {
        if ($_POST)
        {
            $data = Array();
            $data['name'] = strval($_POST['name']);
            $data['id_parent'] = intval($_POST['id_parent']);
            $data['name_url'] = strval($_POST['name_url']);
            $data['meta_title'] = strval($_POST['meta_title']);
            $data['description'] = trim(toolkit::transbadchar(strval($_POST['description'])));
            $data['meta_keywords'] = strval($_POST['meta_keywords']);
            $data['meta_description'] = strval($_POST['meta_description']);
            $data['position'] = intval($_POST['position']);
            $data['date_add'] = time();
            $data['date_upd'] = time();

            $id_category = Category::instances()->set($data);

            if ($id_category)
            {
                if ($_FILES['images']['tmp_name'] != '')
                {
                    $images = Validation::factory($_FILES);
                    if ($images->check())
                    {
                        $images_path = Uploads::handleupload('/uploads/category', $_FILES['images']);
                    }
                    Category::instances()->add_image($id_category, $images_path);
                }

                Message::set('分类添加成功');
                Request::current()->redirect(Url::base() . 'admin/category/add');
            }
        }
        else
        {
            Request::current()->redirect(Url::base() . 'admin/category/add');
        }
    }

    public function action_edit()
    {
        $id = $this->request->param('id');
        $category = ORM::factory('category', $id)->as_array();
        $data = array(
            'category_parent' => $this->categories,
            'data' => $category
        );
        $view = Viewer::display('admin/category_edit', $data);
        $this->response->body($view);
    }

    public function action_do_edit()
    {

        $id = $this->request->param('id');


        if ($_POST)
        {
            $data = Array();
            $data['name'] = strval($_POST['name']);
            $data['id_parent'] = intval($_POST['id_parent']);
            $data['name_url'] = strval($_POST['name_url']);
            $data['meta_title'] = strval($_POST['meta_title']);
            $data['description'] = trim(toolkit::transbadchar(strval($_POST['description'])));
            $data['meta_keywords'] = strval($_POST['meta_keywords']);
            $data['meta_description'] = strval($_POST['meta_description']);
            $data['position'] = intval($_POST['position']);
            $data['date_upd'] = time();

            $id_category = category::instances($id)->update($data);

            if ($id_category)
            {
                if ($_FILES['images']['tmp_name'] != '')
                {
                    $images = Validation::factory($_FILES);
                    if ($images->check())
                    {
                        $images_path = Uploads::handleupload('/uploads/category', $_FILES['images']);
                    }
                    category::instances()->add_image($id, $images_path);
                }

                Message::set('分类修改成功');
                Request::current()->redirect(Url::base() . 'admin/category/edit/' . $id);
            }
        }
        else
        {
            Request::current()->redirect(Url::base() . 'admin/category/edit/' . $id);
        }
    }

    public function action_delete()
    {
        $id = $this->request->param('id');
        $flag = Category::instances($id)->delete();
        if ($flag)
        {
            Message::set('分类删除成功');
            Request::current()->redirect('/admin/category/list');
        }
        else
        {
            Message::set('分类删除失败', 'error');
            Request::current()->redirect('/admin/category/list');
        }
    }

    public function action_do_all()
    {
        if (isset($_POST['id_category']) && $_POST['do_all'] == 'do_delete')
        {

            $arr_id = $_POST['id_category'];

            foreach ($arr_id as $value)
            {
                $id = intval($value);
                $id_category = Category::instances($id)->update(array('active' => 0));
            }

            if ($id_category)
            {
                Message::set('分类删除成功');
                Request::current()->redirect('/admin/category/list');
            }
            else
            {
                Message::set('分类删除失败', 'error');
                Request::current()->redirect('/admin/category/list');
            }
        }
        elseif (isset($_POST['do_all']) && $_POST['do_all'] == 'do_edit')
        {
            $arr_id = $_POST['position'];

            foreach ($arr_id as $key => $value)
            {
                $id = intval($value);
                $id_category = Category::instances($key)->update(array('position' => $value));
            }


            if ($id_category)
            {
                Message::set('顺序修改成功');
                Request::current()->redirect('/admin/category/list');
            }
            else
            {
                Message::set('顺序修改失败', 'error');
                Request::current()->redirect('/admin/category/list');
            }
        }
        else
        {
            Message::set('选择要删除的地区', 'error');
            Request::current()->redirect('/admin/category/list');
        }
    }

}